<template>
  <div>
    <el-dialog
      title="添加管理员"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.rid" placeholder="请选择角色" style="width: 300px">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleOk">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import {createOrUpdateUser, getRoleList} from '@/api/roleApi'

  export default {
    name: "create",
    data() {
      return {
        dialogVisible: false,
        form: {
          name: '',
          account: '',
          password: '',
          rid: ''
        },
        options: []
      }
    },
    methods: {
      show() {
        this.form = {
          name: '',
          account: '',
          password: '',
          rid: ''
        }
        this.dialogVisible = true
        getRoleList().then(res => {
          this.options = res.result
        })
      },
      handleOk() {
        createOrUpdateUser(this.form).then(res => {
          if (res.status === 200) {
            this.$message.success("添加成功")
            this.$emit('Ok')
            this.dialogVisible = false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .el-input {
    width: 300px;
  }
</style>
